﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HousingDistribution
{
    public partial class FormSelectHouse : Form
    {
        public FormSelectHouse()
        {
            InitializeComponent();
            //处理界面布局
            this.dataGridView.Dock = DockStyle.Fill;
            this.dataGridView.Anchor = ((AnchorStyles)
                                        ((((AnchorStyles.Top
                                          | AnchorStyles.Bottom)
                                          | AnchorStyles.Left)
                                          | AnchorStyles.Right)));
            this.buttonExit.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            this.Dock = DockStyle.Fill;
        }

        private void FormSelectHouse_Load(object sender, EventArgs e)
        {
            
        }

        public void SelectDataTable(DataTable dt)
        {
            housingInfo = dt;

            housingInfo.Columns.Add(DataDictionary.Instance().DGVNewColName(DGVNewColConsts.SELECT), typeof(System.Boolean));
            housingInfo.Columns[DataDictionary.Instance().DGVNewColName(DGVNewColConsts.SELECT)].SetOrdinal(1);

            foreach (DataRow dr in housingInfo.Rows)
            {
                dr[DataDictionary.Instance().DGVNewColName(DGVNewColConsts.SELECT)] = false;
            }
            
            dataGridView.DataSource = housingInfo;
        }

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //隐藏列（id 不需要显示）
            ((DataGridView)sender).Columns[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID)].Visible = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            if (rowIndex < 0 || colIndex < 0)
                return;

            DataRow dr = housingInfo.Rows[rowIndex];
            DataColumn dc = housingInfo.Columns[colIndex];

            string colTitle = dataGridView.Columns[colIndex].HeaderText.ToString();

            //不是“选房资格”的item无效
            if (colTitle.Equals(DataDictionary.Instance().DGVNewColName(DGVNewColConsts.SELECT)))
            {
                bool check = Convert.ToBoolean(dataGridView.Rows[rowIndex].Cells[colIndex].EditedFormattedValue);

                //如果选中，则获取对应的房子id
                //将之前选中的checkbox 设为false
                if (check)
                {
                    housingID = Convert.ToInt16(dataGridView.Rows[rowIndex].Cells[DataDictionary.Instance().HousingTableFieldName(HousingColConsts.ID)].EditedFormattedValue);

                    //设置上次选中的checkbox无效
                    dataGridView.Rows[lastSelectedRowIndex].Cells[DataDictionary.Instance().DGVNewColName(DGVNewColConsts.SELECT)].Value = false;
                }
                else
                    housingID = 0;
            }

            lastSelectedRowIndex = rowIndex;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private DataTable housingInfo = null;

        //记录上一次选择的item的RowIndex
        private int lastSelectedRowIndex = 0;

        //获取的房子id
        int housingID = 0;
    }
}
